package com.reluxer.web.controller;

import com.reluxer.service.HelloService;
import com.reluxer.util.IpThreadLocal;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/hello")
public class HelloController {

    @Reference(group = "server1")
    private HelloService helloService1;

    @Reference(group = "server2")
    private HelloService helloService2;

    @ResponseBody
    @RequestMapping("")
    public String hello(HttpServletRequest request) {
        String remoteHost = request.getRemoteHost();
        IpThreadLocal.setIp(remoteHost);
        String hello1 = helloService1.sayHello();
        System.out.println(hello1);
        String hello2 = helloService2.sayHello();
        System.out.println(hello2);

        IpThreadLocal.clear();
        return hello1 + "<br/>" + hello2;
    }

}
